home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 March: Reference Library / Dev.CD Mar 96 RL / Dev.CD Mar 96 RL.toast / Technical Documentation / develop / develop Issue 25 / develop Issue 25 code / QD3D to QTVR / ArticleCode / Source / document.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-11-21  |  4.1 KB  |  134 lines  |  [TEXT/MPCC]

  1. #include "QD3DtoQTVR.h"
  2. #include "extern.h"
  3. #include "document.h"
  4. #include "context.h"
  5. #include "camera.h"
  6. #include "lights.h"
  7.  
  8. DocumentPtr MyNewDocument()
  9. {
  10.     DocumentPtr             theDocument;
  11.     CWindowPtr                theWindow;
  12.     Rect                    theRect = { 0, 0, 16, 16 };
  13.     TQ3Param2D                uvValues = {-1.0, -1.0};
  14.     TQ3DrawContextObject    theDrawContext ;
  15.     TQ3AttributeSet            viewSet;
  16.     Rect                    myBounds = kMyBoundsRect;
  17.     TQ3ColorRGB                clearColor = kMyClearColor;
  18.     TQ3CameraObject            camera = NULL ;
  19.     RGBColor                blackColor = kMyBlackColor;
  20.             
  21.     // create the document record
  22.     theDocument = (DocumentPtr )NewPtrClear(sizeof(DocumentRecord));
  23.     
  24.     // create the window to display the rendering and add referneces to the document
  25.     theWindow = (CWindowPtr)NewCWindow(0L,&myBounds,"\pRendering Window",true,documentProc,(WindowPtr)-1L,true,NULL);
  26.     if (theWindow == nil)
  27.         goto bail;
  28.     theDocument->theWindow = theWindow;
  29.     SetWRefCon((WindowPtr)theWindow, (long)theDocument ) ;    
  30.         
  31.     // create and setup the offscreen buffer
  32.     // ** Notice that QuickDraw 3D prefers direct color
  33.     NewGWorld(&theDocument->drawContextOffscreen, 32, &theWindow->portRect, nil, nil, 0L );
  34.     if (theDocument->drawContextOffscreen == NULL)
  35.         goto bail;
  36.     
  37.     SetGWorld(theDocument->drawContextOffscreen,nil);
  38.     RGBBackColor(&blackColor);
  39.     RGBForeColor(&blackColor);
  40.     EraseRect(&theDocument->drawContextOffscreen->portRect);
  41.  
  42.     // zero out the file spec
  43.     theDocument->theFileSpec.vRefNum = 0 ;
  44.     theDocument->theFileSpec.parID = 0 ;
  45.     theDocument->theFileSpec.name[0] = '\0' ;
  46.     
  47.     // Create the new PixMap draw context
  48.     theDrawContext = MyNewDrawContext( theDocument ) ;
  49.     if (theDrawContext == NULL)
  50.         goto bail;
  51.     
  52.     // Create the view and setup the view attributes    
  53.     theDocument->theView = Q3View_New();
  54.     Q3View_GetDefaultAttributeSet(theDocument->theView, &viewSet);
  55.     Q3AttributeSet_Add(viewSet, 
  56.         kQ3AttributeTypeSpecularColor, &clearColor);
  57.     Q3AttributeSet_Add(viewSet, 
  58.         kQ3AttributeTypeSurfaceUV, &clearColor);
  59.     Q3AttributeSet_Add(viewSet, 
  60.         kQ3AttributeTypeShadingUV, &clearColor);
  61.     Q3Object_Dispose(viewSet);
  62.     Q3View_SetDrawContext(theDocument->theView, theDrawContext);
  63.     Q3Object_Dispose(theDrawContext);
  64.     
  65.     // Initialize the model rotation used for model rotation                        
  66.     Q3Matrix4x4_SetIdentity(&theDocument->modelRotation);
  67.     
  68.     // Add more model and view properties to the document record
  69.     theDocument->documentGroup = nil;
  70.     theDocument->documentGroupScale = 1.0;
  71.     theDocument->currentInterpolation = kQ3InterpolationStylePixel;
  72.     theDocument->illuminationShader = Q3PhongIllumination_New();
  73.     
  74.     // Create the camera and add it to the view
  75.     camera = MyNewCamera( theDocument->theWindow ) ;
  76.     Q3View_SetCamera(theDocument->theView, camera );
  77.     Q3Object_Dispose(camera);
  78.     
  79.     // Add the renderer to the view
  80.     Q3View_SetRendererByType(theDocument->theView, kQ3RendererTypeInteractive);
  81.  
  82.     // Setup the window gworld
  83.     SetGWorld(theWindow,nil);
  84.     
  85.     return(theDocument);
  86.     
  87. bail:
  88.     if (theDocument->drawContextOffscreen)
  89.         DisposeGWorld(theDocument->drawContextOffscreen);
  90.  
  91.     if (theWindow)
  92.         DisposeWindow((WindowPtr) theWindow);
  93.  
  94.     if (theDocument)
  95.         DisposePtr((Ptr)theDocument);
  96.  
  97.     return( (DocumentPtr )nil );
  98. }
  99.  
  100.  
  101. void MyCloseDocument( DocumentPtr theDocument )
  102. {    
  103.     // if we are here we can assume the document has been saved (if required) and 
  104.     // that we can junk all of the data structures associated with the document
  105.  
  106.     // if it has a file associated, close it
  107.     if (theDocument->fRefNum)
  108.         FSClose(theDocument->fRefNum);
  109.  
  110.     // and we can dispose of the window
  111.     if(theDocument->theWindow)
  112.         DisposeWindow((WindowPtr)theDocument->theWindow);
  113.  
  114.     // dispose of our QuickDraw 3d view object
  115.     if(theDocument->theView)
  116.         Q3Object_Dispose(theDocument->theView);
  117.  
  118.     // if we have a group associated with the document, then delete that
  119.     if(theDocument->documentGroup)
  120.         Q3Object_Dispose(theDocument->documentGroup);
  121.  
  122.     if(theDocument->illuminationShader)
  123.         Q3Object_Dispose(theDocument->illuminationShader);
  124.     
  125.     if(theDocument->drawContextOffscreen != nil )
  126.         DisposeGWorld(theDocument->drawContextOffscreen) ;
  127.                 
  128.     // finally dispose of the storage used for the document and 
  129.     // decrement the document count
  130.     if( theDocument != nil )
  131.         DisposPtr((Ptr) theDocument);
  132. }
  133.  
  134.